package com.keba.otp.db.mysql;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.bouncycastle.util.encoders.Hex;

import com.keba.otp.core.Company;
import com.keba.otp.crypto.Crypto;
import com.keba.otp.db.CompaniesDAO;

/**
 * A MySQL adatbazis Companies tablajanak lekerdezesere, modositasara szolgalo DAO 
 * @author vlewir
 *
 */
public class MySQLCompaniesDAO implements CompaniesDAO {

	public MySQLCompaniesDAO() {
		
	}
	
	/**
	 * Visszateriti a tablaban talalhato osszes Company-t
	 */
	@Override
	public List<Company> getAllCompany() {
		return null;
	}

	/**
	 * Megkeresi a Company-t a hozzarendelt hash alapjan
	 * @param hash a hash
	 */
	@Override
	public Company getCompanyByHash(String hash) {
		Company ret = null;
		Connection conn = null;
		try {
			conn = MySQLConnection.getConnection();
			Statement sm = conn.createStatement();
			ResultSet rs = sm.executeQuery("SELECT * from companies WHERE company_hash = \"" + hash + "\"");
			rs.first();
			ret = new Company(rs.getString(1),rs.getString(2),rs.getString(3), Hex.decode(rs.getString(4)));
			rs.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();//log4J
			//throw new DBException();
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		return ret;
	}

	/**
	 * Visszateriti a nevnek megfelelo Company-t
	 * @param n a megkapni kivant Company neve az tablaban
	 */
	@Override
	public Company getCompanyByName(String n) {
		Company ret = null;
		Connection conn = null;
		try {
			conn = MySQLConnection.getConnection();
			Statement sm = conn.createStatement();
			ResultSet rs = sm.executeQuery("SELECT * from companies WHERE company_name = \"" + n + "\"");
			rs.first();
			ret = new Company(rs.getString(1),rs.getString(2),rs.getString(3), Hex.decode(rs.getString(4)));
			rs.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		return ret;
	}

	/**
	 * Beszur egy Company-t az abatbazis Companies tablajaba
	 * @param c a beszurni kivant Company
	 */
	@Override
	public boolean insertCompany(Company c) {
		
		String name = Crypto.performDecrypt(c.getCompanyName(),c.getCompanyKey());
		byte[] inBuff = null;
		try {
			inBuff = name.getBytes("utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		byte[] outBuff = new byte[64];
		org.bouncycastle.crypto.digests.SHA512Digest digest = new org.bouncycastle.crypto.digests.SHA512Digest();
		digest.update(inBuff, 0, inBuff.length);
		int len = digest.doFinal(outBuff, 0);
		String hash = new String(Hex.encode(outBuff, 0, len));
		
		Connection conn = null;
		boolean ret = false;
		try {
			conn = MySQLConnection.getConnection();
			Statement sm = conn.createStatement();
			sm.executeUpdate("INSERT into companies VALUES ( \"" + 
					hash  + "\", \"" +
					c.getCompanyName()  + "\", \"" +
					c.getCompanyPass() + "\", \"" + 
					new String(Hex.encode(c.getCompanyKey()))   + "\")");
			ret = true;
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	/**
	 * Torli a Companyt
	 * @param c a Company melyet kell torolni
	 */
	@Override
	public boolean removeCompany(Company c) {
		Connection conn = null;
		boolean ret = false;
		try {
			conn = MySQLConnection.getConnection();
			Statement sm = conn.createStatement();
			sm.executeUpdate("DELETE FROM companies WHERE company_hash = \"" + c.getCompanyHash() + "\"");
			ret = true;
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	/**
	 * Frissiti a Company jelszavat
	 * @param c a Company melynek a jelszavat kell frissiteni
	 */
	@Override
	public boolean updatePass(Company c) {
		Connection conn = null;
		boolean ret = false;
		try {
			conn = MySQLConnection.getConnection();
			Company temp = getCompanyByName(c.getCompanyName());
			if (temp != null) {
				Statement sm = conn.createStatement();
				sm.executeUpdate("UPDATE companies SET company_pass = \"" + c.getCompanyPass() + "\" WHERE name = \"" + c.getCompanyName() + "\"");
			}
			ret = true;
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

}
